import Vue from 'vue'
import Vuex from 'vuex'

import {
  loginPost
} from '../http/api'
import {
  Login
} from '../http/api'
Vue.use(Vuex)
import {getlist,getSumShop} from '@/http/api.js'
// import {getSumShop} from '@/http/api.js'

// import {
//   chaxun
// } from '../http/api'
Vue.use(Vuex)


export default new Vuex.Store({
  state: {
    user: null,
    //树状门店信息(woo)
    shopList:[],
    //总店信息
    sumShopList:[],
    //分店信息
    subShopList:[],
    //柜台信息：
    couShopList:[]
  },
  treeMenu:{
    menu: null
  },
  // goodsData:{
  //   data:null
  // },
  mutations: {
    setUser(state, user) {
      state.user = user
    },
    quit(state) {
      state.user = null
    },
    
    //引入门店信息到shopList (woo)
    initShopList(state,list){
      // for(let i =0;i < list.length; i++){
      //   if(list[i].match('subbranch')){
      //     console.log('分店数据');
      //     for(let j =0;j < list[i].length; j++){
      //       if(list[i][j].match('subbranch')){
      //         state.sumShopList = list;
      //       }else {
      //         state.subShopList = list;
      //       }
      //     }
      //   }else{
      //     state.couShopList = list;
      //   }
      // }
      state.shopList = list;
    },
    //引入总店信息到sumShopList (woo)
    initSumShopList(state,list){
      state.sumShopList = list
    }
  },
    
  actions: {
    setUserSync(context, user) { //({commit})解构出来
      return loginPost(user) //页面传递过来的数据
        .then(res => {
          console.log('员工登录请求返回的数据：', res)
          if (res.data.value.code == 200) {
            context.commit('setUser', res.data.value.data) //res.data.value =state.user
            //commit('setUser', res.data.user)
            context.commit('setMenu', res.data.value.treeMenu)
            return res.data;
          } else {
            console.log('员工登录错误了')
            return Promise.reject('员工账号密码错误')
          }
        })
    },
    setAdminSync(context, user) {
      return Login(user)
        .then(res => {
          console.log('管理员登录请求返回的数据：', res)
          if (res.data.value.code == 200) {
            context.commit('setUser', res.data.value.data)
            context.commit('setMenu', res.data.value.treeMenu)
            return res.data;
          } else {
            console.log('管理员登录错误了')
            return Promise.reject('管理员账号密码错误')
          }
        })

    },

    //获取树状门店信息(woo)
    getShopList(context){
      getlist().then(res=>{
        console.log(res);
        let data = res.data.data;
        console.log(data)
        context.commit('initShopList',data)
          }).catch(err=>{
              console.log(err)
          })
    },
    //获取总店信息(woo)
    getSumShopList(context){
      console.log(getSumShop)
      
      getSumShop().then(res=>{
        let data = res.data.data;
        context.commit('initSumShopList',data)
        console.log(data)
        // this.sumShopData = res.data.data;
    }).catch(err=>{
        console.log(err)
    })
    },

  },
  modules: {}
})